//
//  KCNewsListVC.m
//  Golden
//
//  Created by king on 2017/8/16.
//  Copyright © 2017年 KC. All rights reserved.
//

#import "KCKit.h"
#import "KCNewsListVC.h"
#import "KCHomeModel.h"

@interface KCNewsListVC ()<UITableViewDelegate,UITableViewDataSource,KCStationDelegate>

@property(nonatomic,strong)KCStation *station;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray <headlines *>*dataArray;

@end

@implementation KCNewsListVC

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNews];
    [self requestNews];
}

- (void)dealloc {
    [KCStation RevokeStation:_station];
    KCLog(@"♻️ Dealloc %@", NSStringFromClass([self class]));
}

#pragma mark - Setup

- (void)createNews{
    
    self.KCTitleLable.text = @"取金头条";
    
    _station = [KCStation RegisterStation:KHttpStationHomeNews delegate:self];
    
    _dataArray = [NSMutableArray array];
    
    _tableView = [[UITableView alloc]initWithFrame:TABLE_FRAME style:TABLE_STYLE_G];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}

- (void)requestNews{
    
    NSDictionary *params = @{@"token":@"CUqu9RmgZJ4qDJ5jQL1l5W057rbQm2sn",
                             @"uid":@"11207",
                             @"start":@"0",
                             @"size":@"20"};
    NSDictionary *KCHttp_Obj = @{KC_URL:[NSString ServiceStr:KCServiceHomeNews],KC_PARAMS_DATA:params};
    [_station sendCargo:KHttpStation_Post cargoID:0x01 cargoObj:KCHttp_Obj];
    
}

#pragma mark - 网络请求回掉

- (id)dispatch:(KCCargo *)cargo{
    
    if (cargo.cargoID == 0x01) {
        
        NSDictionary *dic = (NSDictionary *)cargo.cargoObj;

        NSDictionary *dataDic = dic[@"attachment"];
        
        NSArray *daArray = dataDic[@"headlines"];
        
        [daArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            headlines *model = [headlines modelWithDictionary:obj];
            [_dataArray addObject:model];
            
        }];
        
        [_tableView reloadData];
        
        KCLog(@"%@",dic);
        
        
    }
    
    return nil;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *myCell = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myCell];
    }
    
    cell.textLabel.text = _dataArray[indexPath.row].title;

    cell.textLabel.textColor = [UIColor darkGrayColor];
    
    cell.textLabel.font = SYSTEM_FONT(FONTS_14);
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


@end
